package com.ohos.ringdroid.slice;

import com.ohos.ringdroid.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.photokit.metadata.AVStorage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MyDemoAbilitySlice extends AbilitySlice {
    public ListContainer listContainer = new ListContainer(getContext());
    public ResultSet resultSet;
    private static final String TAG_LOG = "LogUtil";
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, TAG_LOG);

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_media_select_row);

//        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list);

        DataAbilityHelper helper = DataAbilityHelper.creator(getContext());
        try {
            resultSet = helper.query(AVStorage.Audio.Media.EXTERNAL_DATA_ABILITY_URI, null, null);
            HiLog.info(LABEL_LOG, Arrays.toString(resultSet.getAllColumnNames()));
            HiLog.info(LABEL_LOG, String.valueOf((resultSet.getColumnCount())));
            System.out.print(Arrays.toString(resultSet.getAllColumnNames()));

            int number = 0;
            List<String> musicBeans = new ArrayList<>();

            while (resultSet.goToNextRow()) {
                String title = "";
                resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.TITLE));
                title = resultSet.getString(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.TITLE));
                musicBeans.add(title);

            }
            HiLog.info(LABEL_LOG, "musicBeans size: " + musicBeans.size());

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }

    }

    class SettingHolder {

        SettingHolder(Component component) {
        }
    }

    class SampleItemProvider extends BaseItemProvider {
        private List<String> list;
        private AbilitySlice slice;

        SampleItemProvider(List<String> list, AbilitySlice slice) {
            this.list = list;
            this.slice = slice;
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public String getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
            Component component;
            SettingHolder holder;
            if (convertComponent == null) {
                component = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_media_select_row, null, false);
                holder = new SettingHolder(component);
                component.setTag(holder);
            } else {
                component = convertComponent;
                holder = (SettingHolder) component.getTag();
            }


            return component;
        }

    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
